// m_lfit: 等比缩放，限制在指定w与h的矩形内的最大图片。
// m_mfit: 等比缩放，延伸出指定w与h的矩形框外的最小图片。
// m_fill: 固定宽高，将延伸出指定w与h的矩形框外的最小图片进行居中裁剪。
// m_pad: 固定宽高，缩略填充。
// m_fixed: 固定宽高，强制缩略。
// png图片不能加这个处理 加完更大

// resizeImg(640, 150, 'crop')
// resizeImg(640)
// webp/quality,90 图片细节压缩太厉害了
export const resizeImg = (w = 500, h, type = "m_lfit", position='center') => {
    if (type == 'crop') {
        return `?x-oss-process=image/crop,g_${position},w_${w},${ h ? ('h_' + h + ',') : ''}`
    } else {
        return `?x-oss-process=image/resize,${type},w_${w},${ h ? ('h_' + h + ',') : ''}`
    }
}

/**
 * 后端录入做了sanitize
 * @param {string} str 
 */
const ESEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
export const unescapeHTML = (str) => {
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, (all, t) => ESEntities[t]);
}

const BannerOpenType = ['navigate', 'navigate', 'switchTab', 'navigate']

/**
 * banner等位置配置链接地址的解析
 * @param  {[type]} url  [description]
 * @param  {[type]} type [description]
 * @param  {[type]} agt  [description]
 * @return {[type]}      [description]
 */
export const getUrl = (url, type, agt) => {
    url = unescapeHTML(url)
    
    // 地推点逻辑
    if (agt && /\/pages\/item\/item/.test(url)) {
        url += `&agt=${agt}`;
    }

    return {
        url: type == 3 ? '/pages/webpage/index?url=' + url : url,
        openType: BannerOpenType[type]
    }
}


export const numToChDX = (n) => {
    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
        return "数据非法";
    var unit = "仟佰拾亿仟佰拾萬仟佰拾圆角分", str = "";
    n += "00";
    var p = n.indexOf('.');
    if (p >= 0)
        n = n.substring(0, p) + n.substr(p + 1, 2);
    unit = unit.substr(unit.length - n.length);
    for (var i = 0; i < n.length; i++)
        str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
    return str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(萬|亿|圆)/g, "$1").replace(/(亿)萬|壹(拾)/g, "$1$2").replace(/^圆零?|零分/g, "").replace(/圆$/g, "圆整");
};
//加法
export const FloatAdd = (arg1, arg2) => {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}

//减法
export const FloatSub = (arg1, arg2) => {
    var r1, r2, m, n;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

//乘法
export const FloatMul = (arg1, arg2) => {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}


//除法
export const FloatDiv = (arg1, arg2) => {
    var t1 = 0, t2 = 0, r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }

    r1 = Number(arg1.toString().replace(".", ""));

    r2 = Number(arg2.toString().replace(".", ""));
    return (r1 / r2) * Math.pow(10, t2 - t1);
}
// 求余
export const FloatRemainder = (arg1, arg2) => {
    return FloatSub(arg1, FloatMul(parseInt(FloatDiv(arg1, arg2)), arg2))
}


export const priceToYuan = price => {
    return price == 0 ? price : FloatDiv(price, 100).toFixed(2)
}

export const priceToFen = price => {
    return FloatMul(price, 100);
}

/**
 * 转换商品数据字段
 */
export const translateItemData = (item) => ({
    ...item,
    productId: item.id,
    mainImage: item.avatar || item.pics[0],
    productName: item.name,
    productRentPrice: priceToYuan(item.price),
    productRentDay: 1
})